$(function() {
	PubOPT.jsEventLoad(initJsEvent);
});

function initJsEvent() {
	updateState();
	
	$("font[id^='dz_state_']").each(function() {
		var me = $(this);
		if(me.attr("code") == "1") {
			me.html("默认地址");
		} else {
			me.html("[ <a href=\"javascript:setDef('"+me.attr("id").substring(9)+"')\">设置为默认收货地址</a> ]");
		}
	});
	
	$("#account").click(function() {
		if($("#edit_dz").is(":visible")) {
			$.jqalert("请您保存好派送地址后再提交订单！");
			return;
		}
		if($("input[type=radio]:checked").size() == 0) {
			$.jqalert("请您指定派送地址后再提交订单！");
			return;
		}
		
		var id = $("input[type=radio]:checked").val();
		var or_id = (new UUID()).toString();
		var arrSql = [];
		arrSql.push({
			sqlID: 'sjc_confirm_order_9',
			parameters: {
				orders_id: or_id,
				address: $("#"+id+"_address").text(),
				consignee: $("#"+id+"_people").text(),
				tel: $("#"+id+"_tel").text()
			}
		});
		arrSql.push({sqlID: 'sjc_confirm_order_10', parameters: {orders_id: or_id}});
		arrSql.push({sqlID: 'sjc_confirm_order_11', parameters: {orders_id: or_id}});
		
		$("textarea[id^='goods_msg_']").each(function() {
			var me = $(this);
			if($.trim(me.val()).length > 0) {
				arrSql.push({sqlID: 'sjc_confirm_order_12', parameters: {orders_id: or_id, goods_id: me.attr("code"), remark: $.trim(me.val())}});
			}
		});
		arrSql.push({sqlID: 'sjc_confirm_order_13'});
		
		if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))) {
			location.href = basePath + "/p100014.html"; 
		} else {
			$.jqalert("对不起，提交订单失败！");
			return;
		}
	});
	
	if($("div[id^='dz_']").size() == 0) {
		addAddress();
	}
}

function selectRadio(id) {
	$('input[type=radio][value=' + id + ']').attr("checked", true);
	updateState();
}

function updateState() {
	$("div[id^='dz_']").removeClass("tt2").addClass("tt1");
	var sri = $("input[type=radio]:checked").val();
	$("div #dz_"+sri).removeClass("tt1").addClass("tt2");
	
	if(sri != null) {
		$("#txtAddress").text($("#"+sri+"_address").text());
		$("#txtPeople").text($("#"+sri+"_people").text()+"   "+$("#"+sri+"_tel").text());
	}
	
	$("font[id^='btn_edit_']").hide();
	$("#btn_edit_"+sri).show();
	
	if(edit_addr_id != null) {
		$('#tdz_'+edit_addr_id).show();
		edit_addr_id = null;
	}
	$('#edit_dz').hide();
	$("#add_address").hide();
	$("#other_address").show();
}

var edit_addr_id = null;
function editAddress(id) {
	edit_addr_id = id;
	
	$("#address").val($("#"+id+"_address").text());
	$("#people").val($("#"+id+"_people").text());
	$("#tel").val($("#"+id+"_tel").text());
	
	$('#edit_dz').insertAfter('#tdz_'+id);
	$('#tdz_'+id).hide();
	$('#edit_dz').show();
}

function editCancel() {
	$('#edit_dz').hide();
	$('#tdz_'+edit_addr_id).show();
	$("#add_address").hide();
	$("#other_address").show();
	edit_addr_id = null;
}

function saveAddress() {
	var id = edit_addr_id!=null ? edit_addr_id : (new UUID()).toString();
	if(edit_addr_id == null) {
		if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_confirm_order_4", {
			addr_id: id, address: $("#address").val(), people: $("#people").val(), tel: $("#tel").val()
		}))) {
			var addr = $("<div id=\"dz_"+id+"\" class=\"tt2\"><span>" +
							"	<table id=\"tdz_"+id+"\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">" +
							"		<tr>" +
							"			<td width=\"24\" align=\"center\" valign=\"middle\">&nbsp;</td>" +
							"			<td width=\"55\" align=\"left\" valign=\"middle\" class=\"span\">&nbsp;</td>" +
							"			<td width=\"535\" align=\"left\" valign=\"middle\">" +
							"				<a href=\"javascript:selectRadio('"+id+"')\">" +
							"					<input name=\"address\" type=\"radio\" value=\""+id+"\" checked/>" + 
							"					<font id=\""+id+"_address\">"+$("#address").val()+"</font> " +
							"					(<font id=\""+id+"_people\">"+$("#people").val()+"</font>) " +
							"					<font id=\""+id+"_tel\">"+$("#tel").val()+"</font>" +
							"				</a>" +
							"		 	</td>" +
							"			<td width=\"326\" align=\"right\" valign=\"middle\" class=\"span1\">" +
							"				<font id=\"dz_state_"+id+"\" code=\"0\">[<a href=\"javascript:setDef('"+id+"')\">设置为默认收货地址</a>]</font>" +
							"				<font id=\"btn_edit_"+id+"\">&nbsp;&nbsp;&nbsp;[ <a href=\"javascript:editAddress('"+id+"')\">修改本地址</a> ]</font>" +
							"				<font id=\"btn_del_"+id+"\">&nbsp;&nbsp;&nbsp;[ <a href=\"javascript:delAddress('"+id+"')\">删除</a> ]</font>" +
							"			</td>" +
							"		</tr>" +
							"	</table>" +
							"	</span></div>");
			addr.insertBefore("#add_address");
		} else {
			$.jqalert("对不起，保存失败！");
			return;
		}
	} else {
		if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_confirm_order_5", {
			addr_id: id, address: $("#address").val(), people: $("#people").val(), tel: $("#tel").val()
		}))) {
			$("#"+id+"_address").text($("#address").val());
			$("#"+id+"_people").text($("#people").val());
			$("#"+id+"_tel").text($("#tel").val());
		} else {
			$.jqalert("对不起，保存失败！");
			return;
		}
	}
	
	$('#edit_dz').hide();
	$('#tdz_'+edit_addr_id).show();
	$("#add_address").hide();
	$("#other_address").show();
	edit_addr_id = null;
	
	updateState();
}

function addAddress() {
	$("#edit_dz input[type='text']").each(function(){
		$(this).val("");
	});
	$("input[type=radio]:checked").attr("checked", false);
	$("font[id^='btn_edit_']").hide();
	$("div[id^='dz_']").removeClass("tt2").addClass("tt1");
	$('#edit_dz').prependTo('#add_address');
	$("#add_address").show();
	$('#edit_dz').show();
	$("#other_address").hide();
}

function setDef(id) {
	if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction([{sqlID: 'sjc_confirm_order_6'},
		{sqlID: 'sjc_confirm_order_7', parameters: {addr_id: id}}]))) {
		var def = $("font[id^='dz_state_'][code='1']");
		if(def.size() > 0) {
			def.html("[ <a href=\"javascript:setDef('"+def.attr("id").substring(9)+"')\">设置为默认收货地址</a> ]");
			def.attr("code", "0");
		}
		
		var cur = $("#dz_state_"+id);
		cur.html("默认地址");
		cur.attr("code", "1");
	} else {
		$.jqalert("对不起，设置失败！");
		return;
	}
}

function delAddress(id) {
	if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeUpdate("sjc_confirm_order_8", {addr_id: id}))) {
		$('#edit_dz').prependTo('#add_address');
		$("#dz_"+id).remove();
		if($("div[id^='dz_']").size() == 0) {
			addAddress();
		}
	} else {
		$.jqalert("对不起，删除失败！");
		return;
	}
}